<template>
  <!-- 商品模板列表 -->
  <div class="app-container">
    <el-form :model="queryParams" :inline="true" label-width="100px">
      <el-form-item label="商品模板名称">
        <el-input
          v-model="queryParams.mouldName"
          placeholder="请输入商品模板名称"
          size="small"
          style="width: 180px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模板信息">
        <el-input
          v-model="queryParams.mouldData"
          placeholder="请输入商品模板名称"
          size="small"
          style="width: 180px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <!-- <el-form-item label="备注">
        <el-input
          v-model="queryParams.remarks"
          placeholder="请输入备注信息"
          size="small"
          style="width: 160px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-button
        type="primary"
        plain
        icon="el-icon-plus"
        size="mini"
        @click="handleAddOrder"
        >新增</el-button
      >
      <el-button
        type="danger"
        plain
        icon="el-icon-refresh"
        size="mini"
        @click="handleClearCache"
        >刷新</el-button
      >
    </el-row>
    <el-table v-loading="loading" :data="typeList">
      <el-table-column
        label="序号"
        align="center"
        type="index"
        :show-overflow-tooltip="true"
        prop=""
      >
      </el-table-column>
      <el-table-column label="商品模板名称" align="center" prop="mouldName" />
      <el-table-column label="商品模板信息" align="center" prop="goodsInfo">
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remarks">
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="145"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleGoDetail(scope.row)"
            style="margin-right: 5px"
            >详情</el-button
          >
          <el-button size="mini" type="text" @click="handleGoDel(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 新建销售模板 -->
    <el-dialog
      title="新增销售模板"
      :visible.sync="open"
      width="800px"
      append-to-body
      @closed="cancel"
    >
      <div
        class="xian"
        style="
          height: 1px;
          width: 800px;
          background: #eee;
          margin-top: -26px;
          margin-left: -20px;
        "
      ></div>
      <el-form ref="formName" label-width="80px" :model="data">
        <el-row style="margin-top: 15px">
          <el-col
            :span="24"
            style="display: flex; margin-top: -6px; margin-left: -5px"
          >
            <el-form-item label="模板名称">
              <el-input
                v-model="data.mouldName"
                placeholder="请输入模板名称"
                size="small"
                style="width: 180px"
              />
            </el-form-item>
            <!-- <el-form-item label="备注">
              <el-input
                v-model="data.remarks"
                placeholder="请输入备注"
                size="small"
                style="width: 180px"
              />
            </el-form-item> -->
          </el-col>
        </el-row>
        <el-row style="max-height: 450px; overflow: auto">
          <el-col :span="24">
            <el-table
              v-loading="loading"
              :data="groupAddedList"
              style="width: 100%"
            >
              <el-table-column label="序号" align="center" type="index" />
              <el-table-column
                label="商品名称"
                align="center"
                width="160px"
                :show-overflow-tooltip="true"
              >
                <template slot-scope="scope">
                  <input
                    v-if="!scope.row.goodsId"
                    class="table_input"
                    type="text"
                    name=""
                    id=""
                    v-model="keyWord"
                    @input="handleInput"
                    @blur="handleInput"
                    style="
                      padding-left: 5px;
                      width: 130px;
                      outline: none;
                      border: 1px solid #dcdfe6;
                      border-radius: 4px;
                      text-align: center;
                    "
                  />
                  <div class="moudel" v-show="keyWord && !scope.row.goodsId">
                    <div
                      class="row"
                      v-for="(item, index) in goodsList"
                      :key="index"
                      @click="handleChoose(index)"
                    >
                      <span>{{ item.goodsName }}</span
                      ><span class="choosed" v-show="item.isChoosed">已选</span>
                    </div>
                  </div>
                  <span v-if="scope.row.goodsName">{{
                    scope.row.goodsName
                  }}</span>
                </template>
              </el-table-column>

              <el-table-column
                label="售卖单价"
                align="center"
                :show-overflow-tooltip="true"
                prop="goodsPricing"
              >
                <template slot-scope="scope">
                  <input
                    v-if="scope.row.goodsId"
                    class="table_input"
                    type="number"
                    name=""
                    id=""
                    v-model="scope.row.goodsPricing"
                    style="
                      width: 70px;
                      outline: none;
                      border: 1px solid #dcdfe6;
                      border-radius: 4px;
                      text-align: center;
                    "
                  />
                </template>
              </el-table-column>
              <el-table-column
                label="数量"
                align="center"
                :show-overflow-tooltip="true"
                prop=""
              >
                <template slot-scope="scope">
                  <input
                    v-if="scope.row.goodsId"
                    class="table_input"
                    type="number"
                    name=""
                    id=""
                    v-model="scope.row.goodsStock"
                    style="
                      width: 70px;
                      outline: none;
                      border: 1px solid #dcdfe6;
                      border-radius: 4px;
                      text-align: center;
                    "
                  />
                </template>
              </el-table-column>
              <el-table-column label="单位" align="center" prop="unit" />
              <el-table-column label="" align="center">
                <template slot-scope="scope">
                  <span
                    v-if="scope.row.goodsId"
                    @click="handleDel(scope.$index, scope.row)"
                    style="color: #999; font-size: 14px; cursor: pointer"
                    >删除</span
                  >
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm('formName')"
          >添加</el-button
        >
      </div>
    </el-dialog>
    <!-- 模板详情 -->
    <el-dialog
      title="销售模板详情"
      :visible.sync="openDetail"
      width="600px"
      append-to-body
    >
      <el-table v-loading="loading" :data="detailList" style="width: 100%">
        <el-table-column
          label="序号"
          align="center"
          type="index"
          :show-overflow-tooltip="true"
          prop=""
        >
        </el-table-column>
        <el-table-column label="商品名称" align="center" prop="goodsName" />
        <!-- <el-table-column label="供应商名称" align="center" prop="supplierName">
        </el-table-column>
        <el-table-column label="商品分类名称" align="center" prop="className">
        </el-table-column> -->
        <el-table-column label="销售单价" align="center" prop="goodsPricing">
        </el-table-column>
        <el-table-column label="商品数量" align="center" prop="goodsStock">
        </el-table-column>
        <el-table-column label="单位" align="center" prop="unit">
        </el-table-column>
        <!-- <el-table-column label="备注" align="center" prop="remarks">
        </el-table-column> -->
      </el-table>
    </el-dialog>
  </div>
</template>
<script>
import {
  goodsMouldList,
  goodsMouldAdd,
  goodsList,
  goodsProcureDelete,
} from "@/api/goods/index";
export default {
  data() {
    return {
      detailList: [],
      openDetail: false,
      data: {
        remarks: "",
        mouldName: "",
      },
      noPath: false,
      groupAddedList: [{}], //模板列表
      goodsList: [],
      keyWord: "", //输入框关键字
      // 遮罩层
      loading: true,
      // 选中数组
      total: 0,
      // 订单列表
      typeList: [],
      // 是否显示弹出层
      open: false,
      // 搜索状态字典
      statusOptions: [], //支付类型
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mouldId: null,
        mouldName: null,
        mouldData: null,
        remarks: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleGoDel(row) {
      this.$confirm("是否删除该销售模板?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          goodsProcureDelete({ mouldId: row.mouldId }).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getList();
            } else {
              this.$message({
                type: "warning",
                message: res.msg,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleGoDetail(row) {
      //详情
      this.openDetail = true;
      this.detailList = row.goodsList;
    },
    handleInput() {
      //输入事件
      goodsList({ pageNum: 1, pageSize: 20, goodsName: this.keyWord }).then(
        (res) => {
          if (res.code == 200) {
            this.goodsList = res.rows.map((v) => {
              v.isChoosed = false;
              return v;
            });
            if (this.groupAddedList.length) {
              this.groupAddedList.forEach((v, i) => {
                this.goodsList.forEach((item) => {
                  if (item.goodsName == v.goodsName) {
                    item.isChoosed = true;
                  }
                });
              });
            }
          }
        }
      );
    },
    handleChoose(index) {
      //选择哪个
      if (this.goodsList[index].isChoosed) {
        return false;
      }
      this.goodsList[index].isChoosed = true;
      this.groupAddedList.splice(this.groupAddedList.length - 1, 0, {
        goodsName: this.goodsList[index].goodsName,
        goodsPricing: this.goodsList[index].goodsPricing,
        goodsId: this.goodsList[index].goodsId,
        goodsStock: 1,
        unit: this.goodsList[index].unit,
        supplierName: this.goodsList[index].supplierName,
        className: this.goodsList[index].className,
      });
      this.groupAddedList = [...new Set(this.groupAddedList)];
      this.keyWord = ""
    },
    handleClearCache() {
      window.location.reload();
    },
    handleDel(index, row) {
      //删除事件
      this.groupAddedList.splice(index, 1);
      this.goodsList.forEach((v, i) => {
        if (v.goodsName == row.goodsName) {
          this.goodsList[i].isChoosed = false;
        }
      });
    },
    submitForm(formName) {
      this.noPath = false;
      if (!this.data.mouldName) {
        this.$message({
          message: "请输入模板名称",
          type: "warning",
        });
        return false;
      }
      if (this.groupAddedList.length <= 1) {
        this.$message({
          message: "请添加模板",
          type: "warning",
        });
        return false;
      }
      this.groupAddedList.forEach((v) => {
        if (v.goodsPricing == "") {
          this.$message({
            message: "请输入商品价格",
            type: "warning",
          });
          this.noPath = true;
          return false;
        }
        if (v.goodsStock == "") {
          this.$message({
            message: "请输入商品数量",
            type: "warning",
          });
          this.noPath = true;
          return false;
        }
      });
      if (this.noPath) {
        return false;
      }
      this.groupAddedList.forEach((v) => {
        v.goodsStock = Number(v.goodsStock);
        v.goodsPricing = Number(v.goodsPricing);
      });
      this.$refs[formName].validate((valid) => {
        if (valid) {
          goodsMouldAdd({
            mouldName: this.data.mouldName,
            mouldData: JSON.stringify(
              this.groupAddedList.slice(0, this.groupAddedList.length - 1)
            ),
            remarks: this.data.remarks,
          }).then((res) => {
            if (res.code == 200) {
              this.$message({
                message: "添加销售模板成功",
                type: "success",
              });
              this.getList();
              this.groupAddedList = [{}];
              this.data = {
                mouldName: "",
                remarks: "",
              };
              this.open = false;
            }
          });
        } else {
          return false;
        }
      });
    },
    cancel() {
      //创建取消
      this.open = false;
      // this.typeList = []
      this.groupAddedList = [{}];
      this.keyWord = "";
      this.data.mouldName = "";
    },
    handleAddOrder() {
      //新增模板
      this.open = true;
      // this.$router.push({ path: "/commodity/goodsMouldAdd" });
    },
    /** 查询字典类型列表 */
    getList(index) {
      this.loading = true;
      goodsMouldList(this.queryParams).then((response) => {
        this.typeList = response.rows.map((item) => {
          var goodsInfo = [];
          item.goodsList.forEach((v) => {
            // console.log(v.goodsName)
            goodsInfo.push(v.goodsName);
          });
          item.goodsInfo = goodsInfo.join("，");
          return item;
        });
        // console.log(this.typeList)
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      //重置事件
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        mouldId: null,
        mouldName: null,
        mouldData: null,
        remarks: null,
      };
      this.getList();
    },
  },
};
</script>
<style lang="scss" scoped>
.moudel {
  // width: 300px;
  height: 150px;
  overflow: auto;
  background: #f7f7f7;
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-bottom: 13px;
  .row {
    cursor: pointer;
    margin-top: 13px;
    width: 116px;
    height: 25px;
    background: #ffffff;
    border-radius: 8px;
    display: flex;
    padding: 0 8px;
    box-sizing: border-box;
    justify-content: space-between;
    align-items: center;
    .choosed {
      width: 33px;
      height: 18px;
      background: rgba(133, 220, 204, 0.3);
      border-radius: 4px;
      text-align: center;
      line-height: 18px;
      font-size: 12px;
      font-family: Adobe Heiti Std;
      font-weight: normal;
      color: #07b087;
    }
  }
}
::v-deep .el-select__caret {
  line-height: 28px !important;
}
.bottom_scope ::v-deep .el-input__inner {
  outline: line !important;
  height: 28px !important;
  line-height: 28px !important;
  width: 80px !important;
}
.el-range-editor {
  width: 100% !important;
}
.dialog-footer {
  display: flex;
  justify-content: center;
}
.app-container {
  .el-col-11 {
    width: 100%;
  }
  .el-form-item__content {
    position: relative !important;
    span {
      font-size: 16px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #333333;
      position: absolute;
      top: 1px;
      right: 12px;
    }
  }
  .mb8 {
    .title {
      font-size: 20px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #333333;
    }
    .create {
      margin-right: 20 px;
      float: right;
    }
  }
}
</style>